﻿Imports ATS.HelperClass
Imports ATS.CommonClass
Imports ATS.EntryValidation



Partial Public Class ucCategories2
    Dim dbCategories As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Private _pageIsValid As Boolean = True
    Dim editFlag As Integer
    Dim comboClassTextbox As New TextBox
    Dim al As New ArrayList

    Sub pageIsValid(ByVal sender As String, ByVal isValid As Boolean)
        Dim inArray As Boolean = False
        For i = 0 To al.Count - 1
            If al.Item(i) = sender Then
                If isValid Then
                    al.Remove(Trim(sender))
                End If
                inArray = True
                Exit For
            End If
        Next
        If Not inArray Then
            If Not isValid Then
                al.Add(Trim(sender))
            End If
        End If

        If al.Count > 0 Then
            _pageIsValid = False
        Else
            _pageIsValid = True
        End If
    End Sub

    Private Sub ucCategories_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        LoadClassList()
        LoadDepreciationMethod()
        editFlag = HelperClass.EditMode.Load
        LoadCategories()
        DisableAllControls()
        comboClassTextbox = TryCast(comboboxClass.Template.FindName("PART_EditableTextBox", comboboxClass), TextBox)
        AddHandler comboClassTextbox.TextChanged, AddressOf comboBoxClass_TextChanged
    End Sub

    Private Sub DisableAllControls()
        HelperClass.DisableTextBox(gridCategoryDetails)
        HelperClass.DisablePasswordBox(gridCategoryDetails)
        HelperClass.DisableCheckBox(gridCategoryDetails)
        HelperClass.DisableComboBox(gridCategoryDetails)
    End Sub
    Private Sub EnableAllControls()
        HelperClass.EnableTextBox(gridCategoryDetails)
        HelperClass.EnablePasswordBox(gridCategoryDetails)
        HelperClass.EnableCheckBox(gridCategoryDetails)
        HelperClass.EnableComboBox(gridCategoryDetails)
    End Sub
    Private Sub ClearAllControls()
        HelperClass.ClearTextBox(gridCategoryDetails)
        HelperClass.ClearPasswordBox(gridCategoryDetails)
        HelperClass.ClearCheckBox(gridCategoryDetails)
        HelperClass.ClearComboBox(gridCategoryDetails)
    End Sub

    Private Sub LoadCategories(Optional ByVal userFilter As String = "")
        Dim categoryTable = From categoryDetails In dbCategories.Categories _
                         Where categoryDetails.CategoryName.ToUpper.Contains(userFilter.ToUpper) _
                         Order By categoryDetails.CategoryName

        listBoxCategories.ItemsSource = categoryTable
        listBoxCategories.SelectedIndex = 0
        LoadFields(categoryTable)
        If categoryTable.Count = 1 Then
            ExpanderText.Text = categoryTable.Count & " record found"
        Else
            ExpanderText.Text = categoryTable.Count & " records found"
        End If
    End Sub
    Private Sub LoadSelectedCategory(Optional ByVal categoryFilter As String = "")
        Dim categoryTable = From categoryDetails In dbCategories.Categories _
                         Where categoryDetails.CategoryName.ToUpper.Contains(categoryFilter.ToUpper) _
                        Order By categoryDetails.CategoryName

        LoadFields(categoryTable)
    End Sub


    Sub LoadFields(ByVal categoryDetails As IQueryable)
        Dim categoryTable = From catDetails In categoryDetails

        If categoryTable.Count > 0 Then 'And categoryFilter.Length > 0 Then
            With categoryTable.First
                textboxCode.Text = .CategoryCode
                textboxName.Text = .CategoryName
                textboxDescription.Text = .CategoryDesc
                comboboxClass.Text = .AssetClass
                comboboxDepreciationMethod.SelectedValue = .DepreciationMethod
                textboxLife.Text = .Years
            End With
            ucActions.buttonEditMain.IsEnabled = True
            ucActions.buttonDeleteMain.IsEnabled = True
        Else
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            ClearAllControls()
            'textboxCode.Text = Nothing
        End If
        
        pageIsValid(comboboxDepreciationMethod.Name, ValidateCombo(comboboxDepreciationMethod))
        pageIsValid(textboxLife.Name, EntryValidation.ValidateText(textboxLife))
        pageIsValid(comboboxClass.Name, ValidateEditableCombo(comboboxClass, comboClassTextbox))
        pageIsValid(textboxName.Name, EntryValidation.ValidateText(textboxName))
        pageIsValid(textboxCode.Name, EntryValidation.ValidateText(textboxCode))
    End Sub

    Private Sub listBoxCategories_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listBoxCategories.SelectionChanged
        Dim selectedCategory As String = ""
        If Not listBoxCategories.SelectedItem Is Nothing Then
            selectedCategory = listBoxCategories.SelectedItem.CategoryName.ToString
        End If
        LoadSelectedCategory(selectedCategory)
    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchButton.Click
        LoadCategories(searchBox.Text)
    End Sub

    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
        LoadCategories(searchBox.Text)
    End Sub

    Private Sub ucActions_AddButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.AddButtonClicked
        gridListbox.IsEnabled = False
        editFlag = EditMode.Add
        ClearAllControls()
        EnableAllControls()
    End Sub

    Private Sub ucActions_CancelButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.CancelButtonClicked
        Dim selectedCategory As String = ""
        If Not listBoxCategories.SelectedValue Is Nothing Then
            selectedCategory = listBoxCategories.SelectedItem.CategoryName.ToString
        End If

        editFlag = EditMode.Load
        DisableAllControls()
        LoadSelectedCategory(selectedCategory)
        gridListbox.IsEnabled = True
    End Sub

    Private Sub ucActions_DeleteButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.DeleteButtonClicked
        Try
            Dim warningString As String = "Do you really want to delete this record?" & ControlChars.Lf & ControlChars.Lf & listBoxCategories.SelectedItem.categoryname.ToString
            If System.Windows.MessageBox.Show(warningString, "Delete Record", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                Dim selectedCategoryCode As String = listBoxCategories.SelectedItem.categorycode.ToString
                Dim categoriesTable = From categoryDetails In dbCategories.Categories _
                             Where categoryDetails.CategoryCode.ToUpper = (selectedCategoryCode.ToUpper)
                Dim exst As Category = categoriesTable.First
                dbCategories.Categories.DeleteOnSubmit(exst)
                dbCategories.SubmitChanges()
                LoadCategories()
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
        End Try
    End Sub

    Private Sub ucActions_EditButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.EditButtonClicked
        editFlag = EditMode.Edit
        gridListbox.IsEnabled = False
        EnableAllControls()
        textboxCode.IsEnabled = False
    End Sub

    Private Sub ucActions_SaveButtonClicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ucActions.SaveButtonClicked
        Try


            If _pageIsValid Then
                Dim ListBoxSelectedItemIndex As Integer = listBoxCategories.SelectedIndex
                If editFlag = EditMode.Edit Then
                    Dim categoriesTable = From categoryDetails In dbCategories.Categories _
                                 Where categoryDetails.CategoryCode = textboxCode.Text.ToUpper

                    With categoriesTable.First
                        .AssetClass = comboboxClass.Text
                        .CategoryDesc = textboxDescription.Text
                        .CategoryName = textboxName.Text
                        .DepreciationMethod = comboboxDepreciationMethod.SelectedValue
                        .Years = textboxLife.Text
                    End With
                    dbCategories.SubmitChanges()
                ElseIf editFlag = EditMode.Add Then
                    Dim saveNewCategory As New Category
                    With saveNewCategory
                        .AssetClass = comboboxClass.Text
                        .CategoryDesc = textboxDescription.Text
                        .CategoryName = textboxName.Text
                        .DepreciationMethod = comboboxDepreciationMethod.SelectedValue
                        .Years = textboxLife.Text
                        .CategoryCode = textboxCode.Text
                    End With
                    dbCategories.Categories.InsertOnSubmit(saveNewCategory)
                    dbCategories.SubmitChanges()
                End If
                editFlag = EditMode.Load
                If searchBox.Text.Length > 0 Then
                    LoadCategories(searchBox.Text)
                Else
                    LoadCategories()
                End If
                LoadClassList()
                listBoxCategories.SelectedIndex = ListBoxSelectedItemIndex
                DisableAllControls()
                gridListbox.IsEnabled = True
                _pageIsValid = True
            Else
                ucActions.buttonSaveMain.IsEnabled = True
                ucActions.buttonEditMain.IsEnabled = False
                ucActions.buttonAddMain.IsEnabled = False
                ucActions.buttonDeleteMain.IsEnabled = False
                ucActions.buttonCancelMain.IsEnabled = True
                Dim warningString As String = "Some mandatory fields are missing."
                System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
            End If
        Catch ex As Exception
            CheckForSQLErrors(ex)
            ucActions.buttonSaveMain.IsEnabled = True
            ucActions.buttonEditMain.IsEnabled = False
            ucActions.buttonAddMain.IsEnabled = False
            ucActions.buttonDeleteMain.IsEnabled = False
            ucActions.buttonCancelMain.IsEnabled = True
        End Try
    End Sub

    Private Sub LoadClassList()
        Dim classTable = From ClassList In dbCategories.Categories _
                         Group ClassList By ClassList.AssetClass Into Group _
                         Select AssetClass
        comboboxClass.ItemsSource = classTable
        
    End Sub

    Private Sub LoadDepreciationMethod()
        fillDepreciationDropDown(comboboxDepreciationMethod, True, True)
    End Sub

    Private Sub textboxCode_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxCode.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

        If editFlag = EditMode.Add Then
            Dim selectedCategory As String = textboxCode.Text.ToString
            Dim categoryTable = From categoryDetails In dbCategories.Categories _
                         Where categoryDetails.CategoryCode = selectedCategory.ToUpper
            If categoryTable.Count > 0 And textboxCode.IsEnabled = True Then
                _pageIsValid = False
                textboxCode.BorderBrush = New SolidColorBrush(Colors.Red)
                textboxCode.ToolTip = "Code already exist."
            End If
        End If
    End Sub

    Private Sub textboxName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxName.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

    End Sub

    Private Sub comboBoxClass_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
        comboClassTextbox.Text = comboboxClass.Text
        pageIsValid(sender.name, ValidateEditableCombo(comboboxClass, comboClassTextbox))

    End Sub


    'Private Sub comboboxClass_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles comboboxClass.SelectionChanged
    '    _pageIsValid = ValidateCombo(sender)
    'End Sub

    Private Sub comboboxDepreciationMethod_SelectionChanged_1(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles comboboxDepreciationMethod.SelectionChanged
        pageIsValid(sender.name, ValidateCombo(sender))

    End Sub

    Private Sub textboxLife_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxLife.TextChanged
        pageIsValid(sender.name, EntryValidation.ValidateText(sender))

    End Sub
End Class
